package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by Administrator on 2016.8.3.
 */
public class Test1608 extends JFrame {
    public Test1608(){
        MyPanel panel=new MyPanel();
        panel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                panel.setLocation(e.getX(),e.getY(),0);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                panel.setLocation(0,0,1);
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        this.add(panel);
    }

    public static void main(String[] args){
        Test1608 frame=new Test1608();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Welcome to JAVA");
        frame.setLocationRelativeTo(null);
        frame.setSize(300,300);
        frame.setVisible(true);
    }

    private class MyPanel extends JPanel{
        int x;
        int y;
        int z;

        MyPanel(){
        }

        public void setLocation(int x,int y,int z){
            this.x=x;
            this.y=y;
            this.z=z;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if(z==0){
                g.drawString("("+x+","+y+")",x,y);
            }else {
            }
        }
    }
}
